define(function(require, exports, module) {
    var ALL = require("../../common/all"),
        Dialog = ALL.Dialog,
        CONFIG = ALL.CONFIG,
        METHOD = ALL.METHOD,
        Init = ALL.Init;

    var _Dialog = new Dialog({
        title : "提示",
        content : "<p>提交后将进入待申购状态，是否确认提交？</p>",
        text4Btn: ["确定", "取消"],
        class4Btn: ["", "line"]
    });
    var _vm = Init.initVue({
        data : {
            type : 1,
            id : null,
            list: [
                {
                    col_id : '',
                    pnr_no : '',
                    name: '',
                    is_main: null,
                    r_fee: null,
                    total: null,
                    frozen: null,
                    purchase: null
                }
            ],
            count: null,
            check: {},
            purchase: null,
            items : [],
        },
        methods: {
            submit : function() {
                var flag,obj;

                this.check.$valid();
                flag = this.check.flag;

                if (!flag) { return; }

                if (_vm.list.length < 1) {
                    METHOD.alert("请设置发行商");
                    return;
                };

                _vm.list.forEach(function(v){ v.col_id = _vm.id; });

                if (this.sum.length > 0) {
                    _vm.purchase.apply_worth_module = this.sum;
                } else {
                    METHOD.alert("请设置市值模块");
                    return;
                }

                obj = { purchase: _vm.purchase, publisher: _vm.list, id: _vm.id };

                _Dialog.showUI(function(i) {
                    if (i == 0) {
                        METHOD.loading.show();
                        METHOD.ajax("collection/set_params",{data: JSON.stringify(obj)},{type : 'post'}).done(function(){
                            METHOD.loading.hide();
                            METHOD.alert("操作成功").done(function(){
                                window.location.href = "../collection/notissued.html";
                            });
                        }).fail(function(){
                            METHOD.loading.hide();
                        });
                    }
                    this.hideUI();
                });
            },
            add : function() {
                _vm.list.push({col_id: _vm.id, pnr_no : '', name: '', is_main: null, r_fee: null, total: null, frozen: null, purchase: null})
            },
            delete : function(index) {
                _vm.list.splice(index,1);
            },
            getInfoCallback : function(obj, num) {
                var item = this.list[parseInt(num)];
                var arr = this.list.filter(function(v){
                    if (v.pnr_no) {
                        return v.pnr_no == item.pnr_no;
                    } else {
                        return;
                    }
                });
                if (arr.length > 1) {
                    METHOD.alert("不能设置相同的发行商");
                    item.pnr_no = "";
                    return;
                }
                if (obj) {
                    item.name = obj.name;
                } else {
                    item.name = "";
                }
            }
        },
        computed: {
            sum : function() {
                var s = [];

                for (var i = 0, length = this.items.length; i < length; i++) {
                    var item = this.items[i];

                    if (item.selected === true) {
                        s.push(item.id);
                    }
                }
                return s;
            }
        }
    });

    _vm.id = METHOD.getQueryParms("id");
    _vm.count = METHOD.getQueryParms("count");

    METHOD.ajax("coltype/all_list").done(function(json){
        json.data.forEach(function(v){
            v.sub_type.forEach(function(k){
                k.selected = false;
            });
            _vm.items = _vm.items.concat(v.sub_type);
        });
    });


});

